<?php
declare(strict_types=1);

namespace app\controller\api\admin\general;

use think\App;
use think\Request;
use think\exception\ValidateException;

use app\controller\api\ApiBaseController;
use app\logic\api\admin\general\PrintTemplateLogic;
use app\validate\api\admin\general\PrintTemplateValidate;

class PrintTemplateController extends ApiBaseController
{
    protected $logic;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->logic = new PrintTemplateLogic();
    }

    public function index(Request $request)
    {
        $param = $request->param();

        $where = [];
        isset($param['title']) && $param['title'] != '' && $where[] = ['title', 'like', '%' . $param['title'] . '%'];
        isset($param['scene']) && $param['scene'] != '' && $where[] = ['scene', 'like', '%' . $param['scene'] . '%'];
        isset($param['status']) && $param['status'] != '' && $where[] = ['status', '=', $param['status']];

        $lgc_re = $this->logic->list($where);

        return $lgc_re['code'] == 200
            ? $this->success($lgc_re['msg'], $lgc_re['data'])
            : $this->error($lgc_re['msg']);
    }

    public function save(Request $request)
    {
        $data = $request->only([
            'title',
            'scene',
            'content',
            'sort',
            'status',
            'remark'
        ]);

        try {
            validate(PrintTemplateValidate::class)->scene('save')->check($data);

            $lgc_re = $this->logic->save($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }

    public function read($id)
    {
        $lgc_re = $this->logic->read(['id' => $id]);

        return $lgc_re['code'] == 200
            ? $this->success($lgc_re['msg'], $lgc_re['data'])
            : $this->error($lgc_re['msg']);
    }

    public function update(Request $request, $id)
    {
        $data = $request->only([
            'title',
            'scene',
            'content',
            'sort',
            'status',
            'remark',
        ]);
        $data['id'] = $id;

        try {
            validate(PrintTemplateValidate::class)->scene('update')->check($data);

            $lgc_re = $this->logic->update($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }

    public function delete($id)
    {
        $data = $this->request->only([
            'del_ids'
        ]);

        try {
            validate(PrintTemplateValidate::class)->scene('delete')->check($data);

            $lgc_re = $this->logic->delete($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }
}
